package com.cn.transfer.handle.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.entity.DocumentApproval;
import com.cn.transfer.common.entity.fukuan;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.vo.MessagePushVO;
import com.cn.transfer.handle.MessageStrategy;
import com.cn.transfer.mapper.PaymentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2024/6/17 09:35
 * @Description: 收款单审核时
 */
@Slf4j
@Component
public class ReceivingImpl implements MessageStrategy {

    @Autowired
    private PaymentMapper paymentMapper;


    @Override
    public void handle(MessagePushVO message) {
        DocumentApproval documentApproval = ConnmeUtils.mapToObject(message.getBizContent(), DocumentApproval.class);
        List<fukuan> bycode = paymentMapper.byReceiving(documentApproval.getVoucherCode());
        if (bycode.get(0).getOrigAmount()==0){
            return;
        }
        String zhidanren="TC0597|TC0455|"+bycode.get(0).getWxCode();
        if (!bycode.get(0).getWxCode().equals(bycode.get(0).getSalesman())){
            zhidanren=bycode.get(0).getWxCode()+"|"+bycode.get(0).getSalesman();
        }
        StringBuilder danHao=new StringBuilder();
        for (fukuan fukuan : bycode) {
            danHao.append( fukuan.getDocumentCode());
            danHao.append( "，");

        }
        String gettoken = ConnmeUtils.gettoken();
        Header[] originalHeaders = new Header[1];
        originalHeaders[0] = new BasicHeader("Content-Type", "application/json;charset=utf-8");
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("touser", zhidanren);
        parameter.put("msgtype", "text");
        parameter.put("agentid", "1000114");
        Map<String, Object> textContent = new HashMap<>();
        String str="收款单号:"+bycode.get(0).getCode()+"\n收款单位:"+bycode.get(0).getName()+"\n实收金额:"+bycode.get(0).getOrigAmount()+"\n实收本币金额:"+bycode.get(0).getAmount()+"\n对应的销货单号有："+danHao+"\n已完成收款单审核。";
        textContent.put("content", str);
        parameter.put("text", textContent);
        String jsonSale = JSONUtil.toJsonStr(parameter);
        JSONObject jdySale = ApiClient.sendPostRequest("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + gettoken, originalHeaders, jsonSale);
        log.info("收款单审核时提醒==="+jdySale);
    }

    @Override
    public String getMsgType() {
        return "ArapReceive_Audit";
    }
}
